package com.ruge.spring.cloud.gateway.config;

import com.ruge.spring.cloud.gateway.dao.ServerAdminClient;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 嘿丷如歌
 * @version V1.0
 * @Description: AdminTokenClientConfig
 * @date 2021/4/26 20:06
 */
@Configuration
@Import(FeignClientsConfiguration.class)
public class AdminTokenClientConfig {

    @Resource
    private AppConfig appConfig;

    @Bean("serverAdminClientMap")
    @Autowired
    public Map<String, ServerAdminClient> get(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        List<String> services = appConfig.getAdminTokenCheckServices();
        Map<String, ServerAdminClient> result = new HashMap<>(services.size());
        services.forEach((service) -> result.put(
                service,
                Feign.builder().client(client)
                        .encoder(encoder)
                        .decoder(decoder)
                        .contract(contract)
                        .target(ServerAdminClient.class, "http://" + service)
        ));
        return result;
    }
}
